/**
 * Created by limei on 2018/5/12.
 */
import MYCONF from '../myconf';
import eventBus from '../util/event-bus'
import DateTimeHelper from '../util/datetime-helper'

class HomeViewController {
  constructor(_view) {
    this._view = _view;
  }

  getCaseList(type) {
    let timeInter = MYCONF.TIME_INTERVAL;
    let warnList = [];
    let warnCarIds = [];
    let curDate = DateTimeHelper.getCurDate();
   // alert(curDate)
    // let url = './static/data/warns.json';
    let _this = this;
    let url = type === 0 ? MYCONF.service.realWarnList+"?": MYCONF.service.realCompanyWarn + this._view.company_info.id+"&";
    this._view.$http.get(`${url}interval=${timeInter}&curDate=${curDate}`)
      .then(response => {
        response = response.body;
        this._view.warnList = response.warnList;
        this._view.caseList = response.caseList;
        warnList = [...response.warnList,...response.caseList];
        warnList.map((item) => {
          if(warnCarIds.indexOf(item.id)===-1){
            warnCarIds.push(item.id);
          }
        });
        eventBus.$emit("updateWarnCar", warnCarIds);
      }, response => {
        // error callback
      });
  }

  getReverseGeo(item) {
    let url = MYCONF.service.reverseGeocoding;
    this._view.$http.get(url + item.lon + ',' + item.lat)
      .then(response => {
        response = response.body;
        return response.data.city + response.data.dist + response.data.village;
      }, response => {
        // error callback
      });
  }

  getHistoryPath() {
    // let url = './static/data/historyPath.json';
    let time = this._view.single_time;
    let dayTime = ''+time.year+DateTimeHelper.formTime(time.month)+DateTimeHelper.formTime(time.day);
    let startTime = dayTime+'000000';
    let endTime = DateTimeHelper.getNextDay(dayTime)+'000000';
    //let url = `${MYCONF.service.historySingleCar}carNum=${this._view.single_car.id}&dateCur=${startTime}&dateNext=${endTime}`;
    let url = `${MYCONF.service.historySingleCar}carNum=${this._view.single_car.id}&dateCur=${startTime}&dateNext=${endTime}&angleLeavel=5&distance=20`;

    let result = [];
    let itemList = [];
    let pointList = [];
    this._view.historyXData = [];
    this._view.historyYData = [];
    this._view.$http.get(url)
      .then(response => {
        response = response.body;
        if(response.state === 1){
          result = response.result;
          result.map((item, index) => {
            itemList.push(...item.data);
            /*if ((index) % 2 === 0) {
              this._view.historyXData.push(`${index}:00`);
                this._view.historyYData.push(parseInt(item.data[0].speed));
             }*/

              for(let m=0;m<600;m++){
               // if(m%10 == 0 ){
                  let minute = parseInt(m/10);
                  let s = m%10;
                  let second = s * 6;
                  let xTime = `${index}:${minute}:${second}`
                  this._view.historyXData.push(xTime);
                  this._view.historyYData.push(parseInt(item.data[m].speed));
               // }
              }
          });
         /* for(let i=0;i<14400;i++){
            this._view.historyXData.push(i);
          }*/
          this._view.historyXData.push('24:00');
          this._view.historyYData.push(this._view.historyYData[0]);
          itemList.map((item) => {
            let mapCenter = item.point.slice(0, 2);
            pointList.push(mapCenter);
          });
          eventBus.$emit("drawHistoryPath", pointList, result);
        }
        if(response.state === 2){
          eventBus.$emit("singleHistoryNoData");
          eventBus.$emit("clearData");
          eventBus.$emit("noDataTitle");
        }

      }, response => {
        // error callback
      });
  }

  getWarnList(){
    let curTime = ''+this._view.single_time.year+""+DateTimeHelper.formTime(this._view.single_time.month)+""+DateTimeHelper.formTime(this._view.single_time.day);
    let url = ''+MYCONF.service.historyWarList+this._view.single_car.id;
    this._view.$http.get(`${url}&time=${curTime}`)
    // this._view.$http.get('http://117.156.126.6:10003/wuwei/getPreciseAlarmInfoByTimeAndCarNum?car_num=甘H81323&time=20180705')
      .then(response => {
        response = response.body;
        this._view.warnDataObj = response.result?response.data:{};
      }, response => {
        // error callback
      });
  }

  getCarInfo() {
    let url = MYCONF.service.searchCarInfo+this._view.single_car.id;
    let _this = this;
    this._view.$http.get(url)
      .then(response => {
        response = response.body;
        _this._view.updateSingleCar(response);
      }, response => {
        // error callback
      });
  }

  getPathInfo() {
    let url = MYCONF.service.historyPathInfo;
    let time = this._view.single_time;
    let date = ''+DateTimeHelper.formTime(time.year)+DateTimeHelper.formTime(time.month)+DateTimeHelper.formTime(time.day)
    this._view.$http.get(`${url}carNum=${this._view.single_car.id}&date=${date}&speedMin=${MYCONF.SPEED_MIN}&speedMax=${MYCONF.SPEED_MAX}&times=${MYCONF.MILE_TIMES}`)
      .then(response => {
        response = response.body;
        this._view.pathInfo = response.data;
        // eventBus.$emit("updatePathInfo",response.data);
      }, response => {
        // error callback
      });
  }

  getIllegalInfo(sTime,eTime) {
    // let url = './static/data/illegalInfo.json';
    let url = MYCONF.service.realSingleWarn+this._view.single_car.id;
    let curDate = DateTimeHelper.getCurDate();
    this._view.$http.get(`${url}&interval=${MYCONF.TIME_INTERVAL}&curDate=${curDate}`)
      .then(response => {
        response = response.body;
        this._view.illegalInfo = response.alarmInfoTotalBeanList;
      }, response => {
        // error callback
      });
  }

}

export default HomeViewController
